package com.cskaoyan.controller;

import com.cskaoyan.bean.RequestSubmitOrder;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.data.OrderGoods;
import com.cskaoyan.bean.data.WxOrderComment;
import com.cskaoyan.bean.data.WxOrderDetail;
import com.cskaoyan.bean.data.WxOrderSubmit;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.CheckoutVo;
import com.cskaoyan.domain.MarketUser;
import com.cskaoyan.service.CartService;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.service.WxOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    WxOrderService wxOrderService;
    @Autowired
    OrderService orderService;
    @Autowired
    CartService cartService;

    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody WxOrderComment wxOrderComment) {
        wxOrderService.comment(wxOrderComment);
        return BaseRespVo.ok();
    }

    @RequestMapping("list")
    public BaseRespVo list(Integer showType, Integer page, Integer limit) {
        CommonData commonData = wxOrderService.list(showType, page, limit);
        return BaseRespVo.ok(commonData);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        WxOrderDetail wxOrderDetail = wxOrderService.detail(orderId);
        return BaseRespVo.ok(wxOrderDetail);
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        BaseRespVo baseRespVo = wxOrderService.cancel(orderId);
        return baseRespVo;

    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        BaseRespVo baseRespVo = wxOrderService.refund(orderId);
        return baseRespVo;
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        BaseRespVo baseRespVo = wxOrderService.delete(orderId);
        return baseRespVo;
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        BaseRespVo baseRespVo = wxOrderService.confirm(orderId);
        return baseRespVo;
    }

    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId,Integer goodsId) {
       OrderGoods orderGoods = wxOrderService.goods(orderId,goodsId);
        return BaseRespVo.ok(orderGoods);
    }


    /*
    退款
     */
    @PostMapping("submit")
    public BaseRespVo submit(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId=0;
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        CheckoutVo checkoutVo = new CheckoutVo();
        checkoutVo.setAddressId((Integer) map.get("addressId"));
        checkoutVo.setCartId((Integer) map.get("cartId"));
        checkoutVo.setCouponId((Integer) map.get("couponId"));
        checkoutVo.setGrouponRulesId((Integer) map.get("grouponRulesId"));
        checkoutVo.setUserCouponId((Integer) map.get("userCouponId"));
        BaseRespVo cartCheckout = cartService.getCartCheckout(checkoutVo, userId);
        BaseRespVo baseRespVo=orderService.orderSubmit(map, (Map<String, Object>) cartCheckout.getData());
        return baseRespVo;
    }

}
